package com.yanqu.road.dao.impl.activity.metaspace;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.metaspace.CrossMetaSpaceRewardNumData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CrossMetaSpaceRewardNumDaoImpl extends TempDataObjectDao<CrossMetaSpaceRewardNumData> {
    public static String getCrossMetaSpaceRewardNumDataSql = "select * from `t_c_meta_space_reward` where `activityId`=?";
    public List<CrossMetaSpaceRewardNumData> getCrossMetaSpaceRewardNumData(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        List<CrossMetaSpaceRewardNumData> list = new ArrayList<>();
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getCrossMetaSpaceRewardNumDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                CrossMetaSpaceRewardNumData data = parse(rs);
                list.add(data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }

    public static String updateCrossMetaSpaceRewardNumDataSql = "update `t_c_meta_space_reward` set num=?,resetTime=? where activityId=? and conditionId=?";
    @Override
    protected boolean updateData(CrossMetaSpaceRewardNumData crossMetaSpaceRewardNumData) {
        crossMetaSpaceRewardNumData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, crossMetaSpaceRewardNumData.getNum()));
        para.put(i++, new DbParameter(Types.BIGINT, crossMetaSpaceRewardNumData.getResetTime()));
        para.put(i++, new DbParameter(Types.INTEGER, crossMetaSpaceRewardNumData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossMetaSpaceRewardNumData.getConditionId()));
        if (getDb().executeNoneQuery(updateCrossMetaSpaceRewardNumDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertCrossMetaSpaceRewardNumDataSql = "insert into `t_c_meta_space_reward`(activityId,conditionId,num,resetTime) values (?,?,?,?)";
    @Override
    protected boolean addData(CrossMetaSpaceRewardNumData crossMetaSpaceRewardNumData) {
        crossMetaSpaceRewardNumData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, crossMetaSpaceRewardNumData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossMetaSpaceRewardNumData.getConditionId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossMetaSpaceRewardNumData.getNum()));
        para.put(i++, new DbParameter(Types.BIGINT, crossMetaSpaceRewardNumData.getResetTime()));
        if (getDb().executeNoneQuery(insertCrossMetaSpaceRewardNumDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected CrossMetaSpaceRewardNumData parseData(ResultSet rs) throws SQLException {
        CrossMetaSpaceRewardNumData crossMetaSpaceRewardNumData = new CrossMetaSpaceRewardNumData();
        crossMetaSpaceRewardNumData.setActivityId(rs.getInt("activityId"));
        crossMetaSpaceRewardNumData.setConditionId(rs.getInt("conditionId"));
        crossMetaSpaceRewardNumData.setNum(rs.getInt("num"));
        crossMetaSpaceRewardNumData.setResetTime(rs.getLong("resetTime"));

        return crossMetaSpaceRewardNumData;
    }
}
